//
//  NSDate+Extension.m
//  client_confirm
//
//  Created by Seven on 14-7-1.
//  Copyright (c) 2014年 Seven. All rights reserved.
//

#import "NSDate+extension.h"

@implementation NSDate(extension)

+(long) gk_now;
{
    return (long)[[NSDate date] timeIntervalSince1970];
}

+(long long) gk_now64;
{
    return (long long)([[NSDate date] timeIntervalSince1970] * 1000);
}

static int _timezoneHourOffset = 0;
+ (int)timezoneHourOffset{
    if (_timezoneHourOffset == 0) {
        NSTimeInterval tmpN = [self timezoneSecondOffset];
        _timezoneHourOffset = tmpN/3600;
    }
    return _timezoneHourOffset;
}

static int _timezoneSecondOffset = 0;
+ (int)timezoneSecondOffset{
//    if (_timezoneSecondOffset == 0) {
//        _timezoneSecondOffset = [[NSTimeZone systemTimeZone] secondsFromGMT];
//    }
    return _timezoneSecondOffset;
}

static NSDateFormatter *DefaultFormatterYMDHMSSS = nil;//yyyy-MM-dd HH:mm:ss.SSS
static NSDateFormatter *DefaultFormatterYMDHMS = nil;//yyyy-MM-dd HH:mm:ss
static NSDateFormatter *DefaultFormatterYMDHM = nil;//yyyy-MM-dd HH:mm
static NSDateFormatter *DefaultFormatterYMD = nil;//yyyy-MM-dd
static NSDateFormatter *DefaultFormatterMDHMS = nil;//MM-dd HH:mm
static NSDateFormatter *DefaultFormatterMDHM = nil;//MM-dd HH:mm
static NSDateFormatter *DefaultFormatterMD = nil;//MM/dd
static NSDateFormatter *DefaultFormatterM_D = nil;//MM-dd
static NSDateFormatter *DefaultFormatterHM = nil;//HH:mm
static NSDateFormatter *DefaultFormatterHMS = nil;

+ (NSDateFormatter *)formatterWithFormat:(NSString *)format{
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:format];
//    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
//    formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    return formatter;
}

+ (void)initialize{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        DefaultFormatterYMDHMSSS = [self formatterWithFormat:@"yyyy.MM.dd HH:mm:ss.SSS"];
        DefaultFormatterYMDHMS = [self formatterWithFormat:@"yyyy.MM.dd HH:mm:ss"];
        DefaultFormatterYMDHM = [self formatterWithFormat:@"yyyy.MM.dd HH:mm"];
        DefaultFormatterYMD = [self formatterWithFormat:@"yyyy-MM-dd"];
        DefaultFormatterMDHMS = [self formatterWithFormat:@"MM.dd HH:mm:ss"];
        DefaultFormatterMDHM = [self formatterWithFormat:@"MM/dd HH:mm"];
        DefaultFormatterMD = [self formatterWithFormat:@"MM.dd"];
        DefaultFormatterM_D = [self formatterWithFormat:@"MM-dd"];
        DefaultFormatterHM = [self formatterWithFormat:@"HH:mm"];
        DefaultFormatterHMS = [self formatterWithFormat:@"HH:mm:ss"];
    });
}


- (NSString *)FORMAT_MDHM
{
    return [DefaultFormatterMDHM stringFromDate:self];
}

- (NSString *)FORMAT_YMDHMS
{
    return [DefaultFormatterYMDHMS stringFromDate:self];
}

- (NSString *)FORMAT_YMDHM
{
    return [DefaultFormatterYMDHM stringFromDate:self];
}

- (NSString *)FORMAT_YMD
{
    NSString *newTimeStr=[DefaultFormatterYMD stringFromDate:self];
    return newTimeStr;
}

- (NSString *)FORMAT_MDHMS
{
    return [DefaultFormatterMDHMS stringFromDate:self];
}

- (NSString *)FORMAT_MD{
    NSString *newTimeStr=[DefaultFormatterMD stringFromDate:self];
    return newTimeStr;
}

+ (NSString *)fullTimeString{
    NSString *newTimeStr=[DefaultFormatterYMDHMSSS stringFromDate:[NSDate date]];
    return newTimeStr;
}

+ (NSString *)custom_YMDHM:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterYMDHM stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)custom_MDHM:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterMDHM stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)custom_MD:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterM_D stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)custom_YMDHMS:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterYMDHMS stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

- (NSDate *) easyDateFormatter{
    time_t t;
    struct tm tm;
    char *iso8601 = "2016-09-18";
    strptime(iso8601, "%Y-%m-%d", &tm);

    tm.tm_isdst = -1;
    tm.tm_hour = 0;//当tm结构体中的tm.tm_hour为负数，会导致mktime(&tm)计算错误
    t = mktime(&tm);
    
    return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];
}

- (NSString *) easyStringFormatter{
    time_t t = self.timeIntervalSince1970;
    struct tm tm;
    char *iso8601 = "2016-09-18";
    strptime(iso8601, "%Y-%m-%d", &tm);
    
    tm.tm_isdst = -1;
    tm.tm_hour = 0;//当tm结构体中的tm.tm_hour为负数，会导致mktime(&tm)计算错误
    t = mktime(&tm);
    char *s = ctime(&t);
    return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];
}

+ (NSString *)custom_YMD:(long long) seconds
{
    if (seconds > 144913545800) {
        seconds /= 1000;
    }
    return [DefaultFormatterYMD stringFromDate:[NSDate dateWithTimeIntervalSince1970:seconds]];
}

+ (NSString *)recentlyTimeStringWithTimeline:(long long)timeline{
    if (timeline > 144913545800) {
        timeline /= 1000;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeline];
    return [date recentlyTimeString];
}

- (NSString *)recentlyTimeString{
    NSDate *date = self;
    //NSDate *now = [NSDate new];
    NSTimeInterval tmpN = [NSDate timezoneSecondOffset];
    NSTimeInterval delta = -[date timeIntervalSinceNow] + tmpN;
    if (delta < -60 * 10) { // 本地时间有问题
        return [DefaultFormatterYMD stringFromDate:date];
    } else if (delta < 60) { // 1分钟内
        return @"just now";
    } else if (delta < 60 * 60) { // 1小时内
        return [NSString stringWithFormat:@"%d min ago", (int)(delta / 60.0)];
    } else if (date.is_today) {
        return [NSString stringWithFormat:@"%d hour ago", (int)(delta / 60.0 / 60.0)];
    } else {
        return [DefaultFormatterM_D stringFromDate:date];
    }
    /*
    if (date.isYesterday) {
        return [formatterYesterday stringFromDate:date];
    } else if (date.year == now.year) {
        return [formatterSameYear stringFromDate:date];
    } else {
        return [formatterFullDate stringFromDate:date];
    }// */
}
+ (NSString *)listTimeStringWithTimeline:(long long)timeline{
    
    if (timeline > 144913545800) {
        timeline /= 1000;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeline];
    return [date listTimeString];
}

- (NSString *)listTimeString{
    NSDate *date = self;
    if (date.isToday) {
        return [DefaultFormatterHM stringFromDate:date];
    }
    return [DefaultFormatterM_D stringFromDate:date];
}
-(NSString *)FORMAT_HM{
    NSDate *date = self;
    return [DefaultFormatterHM stringFromDate:date];
}
- (NSString *)FORMAT_HMS{
    NSDate *date = self;
    return [DefaultFormatterHMS stringFromDate:date];
}

+ (NSString *)detailTimeStringWithTimeline:(long long)timeline{
    
    if (timeline > 144913545800) {
        timeline /= 1000;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeline];
    return [date detailTimeString];
}

- (NSString *)detailTimeString{
    NSDate *date = self;
    if (date.isToday) {
        return [DefaultFormatterHM stringFromDate:date];
    }
    return [DefaultFormatterMDHM stringFromDate:date];
}


- (NSString *)fullTimeString{
    NSDate *date = self;
    return [DefaultFormatterYMDHMS stringFromDate:date];
}

- (BOOL)is_today{
    NSString *day = [DefaultFormatterYMD stringFromDate:self];
    NSString *today = [DefaultFormatterYMD stringFromDate:[NSDate date]];
    return [day isEqualToString:today];
    //if (fabs(self.timeIntervalSinceNow - [[NSTimeZone localTimeZone] secondsFromGMT]) >= 60 * 60 * 24) return NO;
    //return [NSDate new].day == self.day;
}
+ (NSDate *)getCurrentZoneDate:(NSDate *)date
{
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    return localeDate;
}

//倒计时秒数
- (NSInteger)countDownSecond{
    NSTimeInterval tmpN = [NSDate timezoneSecondOffset];
    NSTimeInterval tmpS = [[NSDate date] timeIntervalSinceNow] - tmpN;
    NSTimeInterval tmp = [self timeIntervalSinceNow] - tmpN;
    return tmp;
}
- (long long)timeInterval{
    NSTimeInterval tmpN = [NSDate timezoneSecondOffset];
    NSTimeInterval tmp = [self timeIntervalSince1970] - tmpN;
    return tmp*1000;
}


/**天数*/
+ (long)dayCount{
    long time = [self gk_now] + [self timezoneSecondOffset];
    long day = time/3600/24;
    return day;
}

@end
